package com.easy.interceptor;

import com.easy.utils.Sys;

import org.apache.ibatis.javassist.bytecode.stackmap.Tracer;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName LoginInterceptor
 * @Description TODO 登录拦截器
 * @Author 周轩霆
 * @Date 2022/5/30 8:55
 **/

public class LoginInterceptor implements HandlerInterceptor {

	/**
	 * @return boolean
	 * @Author 周轩霆
	 * @Description 在执行处理器Handler之前执行
	 * @Date 9:01 2022/5/30
	 * @Param [request, response, handler]
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		HttpSession session = request.getSession();
		Object obj = session.getAttribute(Sys.LOGINSTAFF);
		// 返回true时为放行,此时状态为已登录，不拦截
		boolean hasLogin=obj!=null;
		if (!hasLogin) {
			//此时为非登录状态，拦截
			/*response.setContentType("text/html; charset=UTF-8");
			response.getWriter().print("请先登录！！！！");*/
			session.setAttribute("nologin","nologin");
			response.sendRedirect("../login.html");	
			
		}
		return hasLogin;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}
}
